package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import algorithm.Dynamic_credit;
import algorithm.Point_record;
import DB.DB_connection_pool;


public class Praise extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		JSONObject jsonObj = new JSONObject();
		String dynamic_information_id = request.getParameter("dynamic_information_id");   //获取动态id
		String user_account = request.getSession().getAttribute("user_account").toString();   //用户账号
		
		String select_praise = "select * from praise where praiser_id = '"+user_account+"' and dynamic_information_id = '"+dynamic_information_id+"'";
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(select_praise);
			ResultSet rs = ps_select.executeQuery();
			if(!rs.next()){
				String insert_praise = "insert into praise(praiser_id,dynamic_information_id) values(?,?)";
				PreparedStatement ps_insert = conn.prepareStatement(insert_praise);
				ps_insert.setString(1, user_account);
				ps_insert.setString(2, dynamic_information_id);
				ps_insert.executeUpdate();
				ps_insert.close();
				
				String praise_number = "update dynamic_information set dynamic_information_praise_number = dynamic_information_praise_number+1 where dynamic_information_id = '"+dynamic_information_id+"'";
				PreparedStatement praise_up = conn.prepareStatement(praise_number);
				praise_up.executeUpdate();
				praise_up.close();
				
				jsonObj.put("praise", true);
			}else{
				jsonObj.put("praise", false);
			}
			
			ps_select.close();
			rs.close();
			conn.close();
			jsonObj.put("result", true);
			
			//////////////////--------积分相关，由于之前没设计好，导致这一块代码耦合程度低，再次特别标出------///////////
			Dynamic_credit dc = new Dynamic_credit();
			Map<String, Integer> map = dc.Get_credit(Dynamic_credit.praise);
			
			Point_record pr = new Point_record();
			if(map.get("flag")==1){            //积分改变
			pr.change_point(user_account,(double)(map.get("credit")));
			pr.point_record(user_account, Point_record.praise_event, (double)(map.get("credit")), true);
			}
			
			pr.point_being_change(dynamic_information_id, Point_record.being_praise_event);
			///////////////---------end积分--------////////////////
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jsonObj.toString());
	}

}
